package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"log"
)

type P struct {
	X, Y, Z int
	Name string
}

type Q struct {
	X, Y *int32
	Name string
}



// 重点：在创建Encoder和Decoder时，要传入io.Writer,通过是network connection
// 在调用encode是就会将数据通过io.Writer写入目的地

// this example shows the basic usage of the package: Create an encoder,
// transmit some values, recevie them with a decoder
func main(){
	// Initalize the encoder and decoder. Normally enc and dec would be
	// bound to network connection and the encoder and decoder would run in different processes.
	var network bytes.Buffer  // Stand-in for a network connection
	enc := gob.NewEncoder(&network)  // Will to write to network
	dec := gob.NewDecoder(&network)  // Will to read from network


	// Encode (send) some value
	err := enc.Encode(P{3, 4, 5, "Pythagoras"})
	if err != nil{
		log.Fatal("encode error:", err)
	}

	err = enc.Encode(P{1782,1841, 1922, "Treehouse"})
	if err != nil{
		log.Fatal("encode error:", err)
	}

	// Decode (receive) and print the values.
	var q Q
	err = dec.Decode(&q)
	if err != nil {
		log.Fatal("decode error 2:", err)
	}
	fmt.Printf("%q: {%d, %d}\n", q.Name, *q.X, *q.Y )


	err = dec.Decode(&q)
	if err != nil {
		log.Fatal("decode error 2:", err)
	}
	fmt.Printf("%q: {%d, %d}", q.Name, *q.X, *q.X)







}
